using System;
using UnityEngine;
using UnityEngine.UI;

public class CountdownOutline : MonoBehaviour
{
	[SerializeField]
	private Image m_Image;

	[SerializeField]
	private Image m_PointerImage1;

	[SerializeField]
	private Image m_PointerImage2;

	private Vector3 m_SourceVector;

	private int m_ClockWise;

	[SerializeField]
	private ImageTwinkle m_Twinkle;

	[SerializeField]
	private Color m_NormalColor;

	[SerializeField]
	private Color m_WarningColor;

	public BasePlayer player;

	private PoolRulesStandard m_Rule;

	private void Awake()
	{
		if (m_Image == null)
		{
			m_Image = GetComponent<Image>();
		}
		if (player == null)
		{
			player = GetComponentInParent<BasePlayer>();
		}
		if (!m_PointerImage1)
		{
			m_PointerImage1 = base.transform.GetChild(0).GetComponent<Image>();
		}
		if (!m_PointerImage2)
		{
			m_PointerImage2 = base.transform.GetChild(1).GetComponent<Image>();
		}
		m_Image.color = m_NormalColor;
		m_PointerImage1.color = m_Image.color;
		m_PointerImage2.color = m_Image.color;
		m_Twinkle = m_Image.gameObject.AddComponent<ImageTwinkle>();
		m_Twinkle.SetColor(Color.white, m_WarningColor);
		m_Twinkle.SetTime(0.7f);
		m_Twinkle.SetImage(m_Image, m_PointerImage1, m_PointerImage2);
		m_Twinkle.enabled = false;
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Combine(PoolRulesBase.onNewTurn, new Delegate1Args<int>(OnNewTurn));
		PoolRulesBase.onFireBall = (Delegate0Args)Delegate.Combine(PoolRulesBase.onFireBall, new Delegate0Args(OnFireBall));
		ConstantData.onMaskRiseup = (Delegate0Args)Delegate.Combine(ConstantData.onMaskRiseup, new Delegate0Args(OnMaskRiseup));
		m_Image.color = Color.white * 0f;
	}

	private void Start()
	{
		m_PointerImage1 = base.transform.GetChild(0).GetComponent<Image>();
		m_SourceVector = m_PointerImage1.transform.position - base.transform.position;
		m_ClockWise = ((!m_Image.fillClockwise) ? 1 : (-1));
	}

	private void OnDestroy()
	{
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Remove(PoolRulesBase.onNewTurn, new Delegate1Args<int>(OnNewTurn));
		PoolRulesBase.onFireBall = (Delegate0Args)Delegate.Remove(PoolRulesBase.onFireBall, new Delegate0Args(OnFireBall));
		ConstantData.onMaskRiseup = (Delegate0Args)Delegate.Remove(ConstantData.onMaskRiseup, new Delegate0Args(OnMaskRiseup));
	}

	private void OnMaskRiseup()
	{
		base.enabled = false;
		AudioManager.StopLoopClip("Clock");
	}

	public void SetValue(float fillAmount)
	{
		m_Image.fillAmount = fillAmount;
		float z = fillAmount * 360f * (float)m_ClockWise;
		m_PointerImage1.transform.position = Quaternion.Euler(0f, 0f, z) * m_SourceVector + base.transform.position;
		if (m_Image.fillAmount < 0.25f)
		{
			m_Twinkle.enabled = true;
		}
		if (m_Image.fillAmount <= 0f)
		{
			base.enabled = false;
		}
		else
		{
			base.enabled = true;
		}
		if (m_Twinkle.enabled)
		{
			AudioManager.PlayLoopClip("Clock");
		}
		if (!base.enabled || !m_Twinkle.enabled)
		{
			AudioManager.StopLoopClip("Clock");
		}
	}

	public void OnApplicationPause(bool pause)
	{
		if (pause)
		{
			AudioManager.StopLoopClip("Clock");
		}
	}

	private void OnFireBall()
	{
		m_Twinkle.enabled = false;
		AudioManager.StopLoopClip("Clock");
	}

	private void OnNewTurn(int i)
	{
		if (m_Rule == null)
		{
			m_Rule = (GameManager.Rules as PoolRulesStandard);
		}
		if (m_Rule.CurrentPlayer.GetInstanceID() != player.GetInstanceID())
		{
			iTween.ColorTo(base.gameObject, Color.white * 0f, 0.5f);
			base.enabled = false;
			m_Twinkle.enabled = false;
			return;
		}
		if (!base.enabled)
		{
			AudioManager.Ready();
		}
		base.enabled = true;
		iTween.ColorTo(base.gameObject, m_NormalColor, 0.5f);
	}

	public float GetValue()
	{
		return m_Image.fillAmount;
	}
}
